1   /*
2    * Copyright (C) 2012 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.google;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.SERIALIZABLE;
20  
21  import com.google.common.annotations.GwtCompatible;
22  import com.google.common.annotations.GwtIncompatible;
23  import com.google.common.collect.BiMap;
24  import com.google.common.collect.testing.Helpers;
25  import com.google.common.collect.testing.features.CollectionFeature;
26  import com.google.common.testing.SerializableTester;
27  
28  import java.io.Serializable;
29  import java.lang.reflect.Method;
30  import java.util.Collections;
31  import java.util.List;
32  
33  /**
34   * Tests for the {@code inverse} view of a BiMap.
35   * 
36   * <p>This assumes that {@code bimap.inverse().inverse() == bimap}, which is not technically
37   * required but is fulfilled by all current implementations.
38   * 
39   * @author Louis Wasserman
40   */
41  @GwtCompatible(emulated = true)
42  public class BiMapInverseTester<K, V> extends AbstractBiMapTester<K, V> {
43  
44    public void testInverseSame() {
45      assertSame(getMap(), getMap().inverse().inverse());
46    }
47  
48    @CollectionFeature.Require(SERIALIZABLE)
49    public void testInverseSerialization() {
50      BiMapPair<K, V> pair = new BiMapPair<K, V>(getMap());
51      BiMapPair<K, V> copy = SerializableTester.reserialize(pair);
52      assertEquals(pair.forward, copy.forward);
53      assertEquals(pair.backward, copy.backward);
54      assertSame(copy.backward, copy.forward.inverse());
55      assertSame(copy.forward, copy.backward.inverse());
56    }
57  
58    private static class BiMapPair<K, V> implements Serializable {
59      final BiMap<K, V> forward;
60      final BiMap<V, K> backward;
61  
62      BiMapPair(BiMap<K, V> original) {
63        this.forward = original;
64        this.backward = original.inverse();
65      }
66  
67      private static final long serialVersionUID = 0;
68    }
69  
70    /**
71     * Returns {@link Method} instances for the tests that assume that the inverse will be the same
72     * after serialization.
73     */
74    @GwtIncompatible("reflection")
75    public static List<Method> getInverseSameAfterSerializingMethods() {
76      return Collections.singletonList(getMethod("testInverseSerialization"));
77    }
78  
79    @GwtIncompatible("reflection")
80    private static Method getMethod(String methodName) {
81      return Helpers.getMethod(BiMapInverseTester.class, methodName);
82    }
83  
84  }